package io.adrainty.algorithm.mars;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 寻找最大葫芦
 *
 * @author AdRainty
 * @version V1.0.0
 * @link <a href="https://www.marscode.cn/practice/364l85wv6oo6lq?problem_id=7414004855077584940">...</a>
 * @since 2024/11/21 下午10:32
 */
public class Solution005 {

    private Solution005() {}

    public static int[] solution(int n, int max, int[] array) {
        // Edit your code here
        int[] map = new int[15];
        for (int i = 0; i < n; i++) {
            // 将 1 转换为 14 因为 A 比 K 大
            if (array[i] == 1) map[14]++;
            else map[array[i]]++;
        }
        System.out.println(Arrays.toString(map));

        List<Integer> over3Numbers = new ArrayList<>();
        for (int i = map.length - 1; i >= 0; i--) {
            if (map[i] >= 3) {
                int num = i == 14 ? 1 : i;
                over3Numbers.add(num);
            }
        }

        for (int number: over3Numbers) {
            for (int j = map.length - 1; j >= 0; j--) {
                int num2 = j == 14 ? 1 : j;
                if (number == num2) continue;
                if (map[j] >= 2) {
                    int sum = number * 3 + num2 * 2;
                    if (sum <= max) {
                        System.out.println(Arrays.toString(new int[]{number, num2}));
                        return new int[]{number, num2};
                    }
                }
            }
        }

        return new int[]{0, 0};
    }

}
